import { getTSFromPb, converterFunc, toString } from "mmb";
import * as google_protobuf_timestamp_pb from "google-protobuf/google/protobuf/timestamp_pb";
import * as sharedpb from "../grpc-client/shared/message_pb";
import * as commonactivitypb from "../grpc-client/shared/common_activity_pb";
import {
  ProductGroup,
  ProductSpec,
  ProductAttributeDef,
  ProductWithDistributeInfo,
  CheckIn1,
  Lottery1,
} from "./declareTypes";

export const numberZeroValue = (n: number): number | undefined => {
  return n > 0 ? n : undefined;
};

export const jsTimestampToPb = converterFunc(
  (ts: number): google_protobuf_timestamp_pb.Timestamp => {
    const pbts = new google_protobuf_timestamp_pb.Timestamp();
    // pbts.setSeconds(ts / 1000);
    // pbts.setNanos((ts % 1000) * 1e6);
    pbts.fromDate(new Date(ts));
    return pbts;
  }
);

export const pbTimestampToJs = converterFunc(
  (pbts: google_protobuf_timestamp_pb.Timestamp): number => {
    return getTSFromPb(pbts.getSeconds(), pbts.getNanos());
  }
);

export const productGroupPbToObject = (pb: sharedpb.ProductGroup): ProductGroup => {
  return {
    productGroupId: `${pb.getId()}`,
    brandId: `${pb.getBrandId()}`,
    sortNo: pb.getSortNo(),
    name: pb.getName(),
    productCount: pb.getProductCount(),
  };
};

export const productAttributeDefPbToObject = (
  pb: sharedpb.ProductAttributeDef
): ProductAttributeDef => {
  return {
    key: pb.getKey(),
    values: pb.getValuesList(),
  };
};

const productSpecPbToObject = (pb: sharedpb.ProductSpec): ProductSpec => {
  return {
    productSpecId: `${pb.getId()}`,
    productAttributes: JSON.parse(pb.getProductAttributes()),
    name: pb.getName(),
    price: pb.getPrice(),
  };
};

export const productWithDistributeInfoPbToObject = (
  pb: sharedpb.ProductWithDistributeInfo
): ProductWithDistributeInfo => {
  return {
    productId: toString(pb.getProductId()),
    name: pb.getName(),
    pics: pb.getPicsList(),
    hasSpecs: pb.getHasSpecs(),
    priceMin: pb.getPriceMin(),
    priceMax: pb.getPriceMax(),
    productGroupId: toString(pb.getProductGroupId()),
    productAttributeDefs: pb
      .getProductAttributeDefsList()
      .map(productAttributeDefPbToObject),
    productSpecs: pb.getProductSpecsList().map(productSpecPbToObject),
    percentSale: pb.getPercentSale(),
    percentOff: pb.getPercentOff(),
  };
};

export const checkIn1PbToObject = (
  pb: commonactivitypb.CheckIn1Entity
): CheckIn1 => {
  return {
    awardOnceCoins: pb.getAwardOnceCoins(),
    awardReachMaxCoins: pb.getAwardReachMaxCoins(),
    continuous: pb.getContinuous(),
  };
};

export const lottery1PbToObject = (
  pb: commonactivitypb.Lottery1Entity
): Lottery1 => {
  return {
    uiType: pb.getUIType(),
    winRate: pb.getWinRate(),
    prizeRates: pb.getPrizeRatesList(),
  };
};

export const checkIn1ObjectToPb = (o: CheckIn1) => {
  const pb = new commonactivitypb.CheckIn1Entity();
  pb.setAwardOnceCoins(o.awardOnceCoins);
  pb.setAwardReachMaxCoins(o.awardReachMaxCoins);
  pb.setContinuous(o.continuous);
  return pb;
};

export const lottery1ObjectToPb = (o: Lottery1) => {
  const pb = new commonactivitypb.Lottery1Entity();
  pb.setUIType(o.uiType);
  pb.setWinRate(o.winRate);
  pb.setPrizeRatesList(o.prizeRates);
  return pb;
};
